<?php
namespace Framework\Library;

class Tool
{
    /**
     * @return bool
     */
    public static function validateDateTime($date, $format)
    {
        $d = \DateTime::createFromFormat($format, $date);

        return $d && $d->format($format) == $date;
    }

    public static function secondsLeftToday()
    {
        $now = time();
        $todayEnd = strtotime(date('Y-m-d', $now + 3600 * 24));

        return $todayEnd - $now;
    }

    public static function secondsLeftThisHour()
    {
        $now = time();
        $thisHourEnd = strtotime(date('Y-m-d H:00:00', $now + 3600));

        return $thisHourEnd - $now;
    }

    public static function ip2long($ip)
    {
        $ipChunks = explode('.', $ip, 4);
        foreach ($ipChunks as $i => $v) {
            $ipChunks[$i] = abs(intval($v));
        }

        return sprintf('%u', ip2long(implode('.', $ipChunks)));
    }

    /**
     * 获取配置文件.
     *
     * @return array
     */
    public static function getConfig($configPath, $key)
    {
        if (empty($key)) {
            return [];
        }

        $configFilePath = $configPath . DIRECTORY_SEPARATOR . lcfirst($key) . '.php';
        if (!file_exists($configFilePath)) {
            return [];
        }

        $config = require $configFilePath;
        if (!is_array($config)) {
            return [];
        }

        return $config;
    }

    public static function getExtension($filePath)
    {
        $supportedMimeTypeExtensions = [
            'image/png' => '.png',
            'image/jpeg' => '.jpeg',
            'image/gif' => '.gif',
            'audio/mp4' => '.m4a',
            'audio/x-hx-aac-adts' => '.aac',
            'audio/aac' => '.aac',
            'video/mp4' => '.mp4',
            'video/3gpp' => '.mp4',
            'video/quicktime' => '.mp4',
        ];

        $mimeType = self::getMimeType($filePath);
        if (!isset($supportedMimeTypeExtensions[$mimeType])) {
            return false;
        }

        return $supportedMimeTypeExtensions[$mimeType];
    }

    public static function getMimeType($filename)
    {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        if (!$finfo) {
            return false;
        }
        if (!file_exists($filename)) {
            return false;
        }
        $mimeType = finfo_file($finfo, $filename);
        finfo_close($finfo);

        return $mimeType;
    }

    public static function generateUniqueString(int $length) : string
    {
        if (function_exists('openssl_random_pseudo_bytes')) {
            $randomData = openssl_random_pseudo_bytes($length / 2);
            if ($randomData !== false && strlen($randomData) === $length / 2) {
                return bin2hex($randomData);
            }
        }
        if (@file_exists('/dev/urandom')) { // Get 100 bytes of random data
            $randomData = file_get_contents('/dev/urandom', false, null, 0, $length / 2);
            if ($randomData !== false && strlen($randomData) === $length / 2) {
                return bin2hex($randomData);
            }
        }
        // Last resort which you probably should just get rid of:
        $randomData = mt_rand() . mt_rand() . mt_rand() . mt_rand() . microtime(true) . uniqid(mt_rand(), true);

        return substr(hash('sha512', $randomData), 0, $length);
    }
}
